﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: frmRegisterSubGroup.cs
#endregion

using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.finan.entries
{
	/// <summary>
	/// Description of frmRegisterGroup.
	/// </summary>
	public partial class frmRegisterSubGroup : csharp.common.RegisterTemplate
	{
		private SubGroup subgroup;
		private SubGroups listSubgroups;
		private Groups    listGroups;
		
		public frmRegisterSubGroup()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			subgroup = new SubGroup();
			listSubgroups  = new SubGroups();
			listGroups     = new Groups();
		}
		
		protected override void newEvent()
		{
			subgroup.clear();
			txtCode.Focus();
		}		
		
		/// <summary>
		/// Set data from edits to class
		/// </summary>
		protected override void setClass()
		{
			subgroup.code= txtCode.Text;
			subgroup.name= txtGroup.Text;
			subgroup.active= cbxActive.Checked;
			//subgroup.groupId= listGroups.get(cbxGroup.SelectedIndex).id;
			subgroup.groupId = (cbxGroup.SelectedItem as Group).id;
		}
		
		/// <summary>
		/// Set data from class to edits
		/// </summary>
		protected override void setEdits()
		{
			subgroup.id= dgvMain.CurrentRow.Cells["id"].Value.ToString();
			
			if (subgroup.get())
			{
				txtCode.Text= subgroup.code;
				txtGroup.Text= subgroup.name;
				cbxActive.Checked= subgroup.active;
				//listGroups.search(subgroup.groupId);
				//cbxGroup.SelectedIndex= listGroups.getIndex();
				Functions.setSelectIndexComboGroup(cbxGroup, subgroup.groupId);
			}
		}
		
		/// <summary>
		/// Fill the DataGridView
		/// </summary>
		protected override void fillDataGridView()
		{
			int tam;
			
			listSubgroups.fillList();
			dgvMain.Rows.Clear();
			tam= listSubgroups.count();
			for (int i=0; i < tam; i++)
			{
				String []row= {listSubgroups.get(i).id, listSubgroups.get(i).code, listSubgroups.get(i).name};
				dgvMain.Rows.Add(row);
			}
		}
		
		/// <summary>
		/// Find the object of search
		/// </summary>
		protected override void find()
		{
			common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
		}		
		
	   /// <summary>
	   /// Save the register in the database.
	   /// </summary>
	   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
	   protected override bool save()
	   {
	   	return subgroup.save();
	   }
	   
	   /// <summary>
	   /// Update the register in the database
	   /// </summary>
	   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
	   protected override bool update()
	   {   
	   	return subgroup.update();
	   }
	   
	   /// <summary>
	   /// Delete the register in the database.
	   /// </summary>
	   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
	   protected override bool delete()
	   {
	   	subgroup.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
	   	return subgroup.delete();
	   }
	   
	   /// <summary>
	   /// Method call in initialization
	   /// </summary>
	   protected override void init()
	   {
	   	KryptonDataGridViewTextBoxColumn id   = new KryptonDataGridViewTextBoxColumn();
	   	KryptonDataGridViewTextBoxColumn code = new KryptonDataGridViewTextBoxColumn();
	   	KryptonDataGridViewTextBoxColumn name = new KryptonDataGridViewTextBoxColumn();
	   	
	   	dgvMain.Columns.AddRange(new DataGridViewColumn[] {id,code,name});
	   	
	   	id.Name= "id";
	   	id.Visible= false;
	   	
	   	code.Name = "code";
	   	code.HeaderText= "Código";
	   	
	   	name.Name= "name";
	   	name.Width= 300;
	   	name.HeaderText = "Nome";
	   	
	   	//Fill groups
	   	listGroups.fillList();
	   	int tam= listGroups.count();
	   	for (int i=0; i < tam; i++)
	   		cbxGroup.Items.Add(listGroups.get(i));
	   	cbxGroup.DisplayMember = "name";
	   	cbxGroup.ValueMember = "id";
	   }		
		
		private void TabPrincipalEnter(object sender, EventArgs e)
		{
			txtCode.Focus();
		}
	}
}
